package com.algorithm.hqy;

/**
 * @Classname LinkedListStack
 * @Date 2019/9/18 15:49
 * @auther by hqy
 */
public class LinkedListStack {
    private int maxSize;

    private DataNode top;

    public LinkedListStack(int maxSize) {
        this.maxSize = maxSize;
    }

    public boolean isEmpty() {
        return top == null;
    }

    public boolean isFull() {
        int count = 0;
        DataNode temp = top;
        while (true) {
            if (temp != null) {
                count++;
            } else {
                break;
            }
            temp = temp.getNext();
        }
        return count == maxSize;
    }

    public void push(int num) {
        if (isFull()) {
            System.out.println("栈已经满了~~~~");
            return;
        }
        DataNode data = new DataNode(num);
        data.setNext(top);
        top = data;

    }

    public void pop() {
        if (isEmpty()) {
            System.out.println("栈已经空了~~~~");
            return;
        }
        System.out.println(top);
        top = top.getNext();
    }

    public void show() {
        if (isEmpty()) {
            System.out.println("栈已经空了~~~~");
            return;
        }
        DataNode temp = top;
        while (true) {
            if (temp != null) {
                System.out.println(temp);
            } else {
                System.out.println("没有啦~~~~~~");
                break;
            }
            temp = temp.getNext();

        }

    }


}


class DataNode {
    private int value;

    public DataNode(int value) {
        this.value = value;
    }

    private DataNode next;

    public DataNode getNext() {
        return this.next;
    }

    public void setNext(DataNode next) {
        this.next = next;
    }

    @Override
    public String toString() {
        return this.getClass().getName() + ",value:" + value;
    }
}
